

from Commons import *
from Creature import *
from Map import *
from Maps import *
#game class
#provides an interface for saving/loading, and holding objects
class TheSevenOrbsGame:
    order = [A_MAP]#determines order for saving and loading
    special = [A_PLAYER]#things that are loaded from other things
    gameitems = [None for item in order]+[None for item in special]#where the items are
    def __init__(self, loadFile=None):
        self.newGame = False
        if (loadFile==None):#new game
            self.newGame = True
        else:
            try:#ugly loop for now XXX: game should tell why something cannot be loaded
                self.load(loadFile)
            except IOError:
                print "File not found, new game"
                self.newGame = True
            except Exception:#
                traceback.print_exc()
                print "Error, could not load file, new game"
                self.newGame = True#used to tell ChronoGame to start a new game
        if (self.newGame):#new game
            self[A_PLAYER] = Player(10,13)#place the player
            self[A_MAP] = None
        else:#get other things from the attributes of what is loaded
            self[A_PLAYER] = self[A_MAP][L_CRE].getPlayer()
        
    def __getitem__(self, key):
        if key in self.order:#key is in self.order
            return self.gameitems[self.order.index(key)]
        if key in self.special:#key in self.special
            return self.gameitems[len(self.order)+self.special.index(key)]
        raise KeyError#key not found
    def __setitem__(self,key,newitem):
        if key in self.order:#key in self.order
            self.gameitems[self.order.index(key)] = newitem
            return
        if key in self.special:#key in self.special
            self.gameitems[len(self.order)+self.special.index(key)] = newitem
            return
        raise KeyError#key not found
    def save(self,filename):#save the game
        FILE = open(filename, 'w')
        for item in self.order:
            cPickle.dump(self[item], FILE)
            try:#special sub-pickling method if the class has it
                self[item].save(FILE)
            except AttributeError:
                pass
        FILE.close()

    def load(self,filename):#load everything with cPickle
        FILE = open(filename, 'r')
        for item in self.order:
            self[item] = cPickle.load(FILE)
            try:#special sub-pickling method if the class has it
                self[item].load(FILE)
            except AttributeError:
                pass
        FILE.close()
if __name__=="__main__":
    #set font and screen
    L.console_set_custom_font(font,flags=L.FONT_LAYOUT_ASCII_INROW|L.FONT_TYPE_GRAYSCALE)
    L.console_init_root(SCREEN_WIDTH,SCREEN_HEIGHT,"The Seven Orbs")
    #load the game
    setGame(TheSevenOrbsGame(FILE_PATH))
    
    if (Game().newGame):#if new game
        switchMap(MapGenBasic.MapGenBasic(100,100))#set the map, change to load different maps
        Game()[A_MAP].readMap()#initialize the first map
    else:#otherwise
        Game()[A_MAP].restoreMap()#restore the map
    #set the FPS
    L.sys_set_fps(GAME_FPS)
    L.console_set_foreground_color(0,L.white)
    exitGame = False#should the game be exited

    #put the player on the map
    i = 0#iterator
    while (True):#do everything
        if (i >= TURN_REFRESH):#refresh the screen after N turns
            updateDisplay()
            i=0
        Game()[A_MAP].tick()#tick the layers
        i+=1#iterate i for turn refresh
